package com.craftplay.controller;

import com.craftplay.VO.CommentVO;
import com.craftplay.VO.UnitVOPageInfo;
import com.craftplay.common.ServerResponse;
import com.craftplay.model.*;
import com.craftplay.service.BehaveService;
import com.craftplay.service.CommentService;
import com.craftplay.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/play/comment")
public class CommentController {

    @Autowired
    HostHolder hostHolder;

    @Autowired
    UserService userService;

    @Autowired
    CommentService commentService;

    @Autowired
    BehaveService behaveService;

    @RequestMapping(method = RequestMethod.POST, value = "/add")
    @ResponseBody
    public ServerResponse<Comment> addComment(@RequestBody Comment comment) {
        User user = hostHolder.getUser();
        if (user != null) {
            int userId = hostHolder.getUser().getUserId();
            if (comment != null) {
                comment.setFromId(userId);
                comment.setCreatedTime(new Date());
                if (commentService.insertOneRecord(comment) > 0) {

                    BehaveModel behaveModel = new BehaveModel();
                    behaveModel.setUserId(comment.getToId());
                    behaveModel.setBuserId(comment.getFromId());
                    behaveModel.setUnitId(comment.getParentId().intValue());
                    behaveModel.setBehave("评论");
                    behaveModel.setCreateTime(new Date());
                    behaveService.insertBehave(behaveModel);


                    return ServerResponse.createBySuccess(comment);
                } else {
                    return ServerResponse.createByErrorMessage("添加失败！");
                }
            }
            return ServerResponse.createByErrorMessage("添加失败！");
        }
        return ServerResponse.createByErrorMessage("添加失败！");
    }

    @RequestMapping(method = RequestMethod.GET, value = "/delete")
    @ResponseBody
    public ServerResponse<String> deleteComment(Long commentId) {
        if (commentId > 0) {
            if (commentService.deleteCommentByCommentId(commentId) > 0) {
                return ServerResponse.createBySuccessMessage("删除成功");
            } else {
                return ServerResponse.createByErrorMessage("删除失败！");
            }
        }
        return ServerResponse.createByErrorMessage("删除失败！");
    }

    /**
     * 获取活动的所有评论
     *
     * @param unitId
     * @return
     */

    @RequestMapping(method = RequestMethod.GET, value = "/getUnitDetailComment")
    @ResponseBody
    public ServerResponse<List<CommentVO>> getUnitDetailComment(int unitId) {
        User thisUser = hostHolder.getUser();
        List list = new ArrayList();
        List<CommentVO> unitDetailComment = commentService.getUnitDetailComment(unitId, thisUser.getUserId());
        if (unitDetailComment != null) {
            list.add(unitDetailComment);
            return ServerResponse.createBySuccess(unitDetailComment);
        } else {
            return ServerResponse.createByErrorMessage("查找失败！");
        }
    }


    /**
     * 获取所有未读评论
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/getNotReadComment")
    @ResponseBody
    public ServerResponse<List<Unit>> getNotReadComment() {
        User thisUser = hostHolder.getUser();
        List<Unit> notReadComment = commentService.getNotReadComment(thisUser.getUserId());
        return ServerResponse.createBySuccess(notReadComment);
    }


}
